package com.zhiyume.service;

import com.zhiyume.dao.api.TDramaMapper;
import com.zhiyume.dao.api.TDramaSelfMapper;
import com.zhiyume.dao.entity.TDrama;
import com.zhiyume.dao.entity.TDramaCriteria;
import com.zhiyume.dao.entity.TDramaWithBLOBs;
import com.zhiyume.service.util.DateUtil;
import com.zhiyume.service.util.GenerateKeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class IDramaService {
    @Autowired
    TDramaMapper tDramaMapper;

    @Autowired
    TDramaSelfMapper tDramaSelfMapper;

    /**
     * 根据id获取单部剧
     *
     * @param id
     * @return
     */
    public TDramaWithBLOBs findDramaById(long id) {
        TDramaCriteria example = new TDramaCriteria();
        TDramaCriteria.Criteria c = example.createCriteria();
        c.andIdEqualTo(id);
        List<TDramaWithBLOBs> tDramas = tDramaMapper.selectByExampleWithBLOBs(example);
        if (null != tDramas && tDramas.size() > 0) {
            return tDramas.get(0);
        }
        return new TDramaWithBLOBs();
    }

    /**
     * 根据剧名称获取剧列表
     *
     * @param dramaName
     * @return
     */
    public List<TDrama> findDramasListByName(String dramaName) {
        TDramaCriteria example = new TDramaCriteria();
        TDramaCriteria.Criteria c = example.createCriteria();
        c.andNameEqualTo(dramaName);
        List<TDrama> tDramas = tDramaMapper.selectByExample(example);
        return tDramas;
    }

    /**
     * 根据导演获取剧列表
     *
     * @param diretor
     * @return
     */
    public List<TDrama> findDramasListByDirector(String diretor) {
        TDramaCriteria example = new TDramaCriteria();
        TDramaCriteria.Criteria c = example.createCriteria();
        c.andDirectorLike("%" + diretor + "%");
        List<TDrama> tDramas = tDramaMapper.selectByExample(example);
        return tDramas;
    }

    /**
     * 根据剧类别获取剧列表
     *
     * @param dramaType
     * @return
     */
    public List<TDrama> findDramasListByType(int start, int end, String dramaType) {
        TDramaCriteria example = new TDramaCriteria();
        TDramaCriteria.Criteria c = example.createCriteria();
        example.setLimitStart(start);
        example.setLimitEnd(end);
        example.setOrderByClause(" update_time desc");
        c.andDramaTypeEqualTo(Integer.parseInt(dramaType));
        List<TDrama> tDramas = tDramaMapper.selectByExample(example);
        return tDramas;
    }

    /**
     * 根据出品公司获取剧列表
     *
     * @param company
     * @return
     */
    public List<TDrama> findDramaListByCompany(String company) {
        TDramaCriteria example = new TDramaCriteria();
        TDramaCriteria.Criteria c = example.createCriteria();
        c.andCommpanyEqualTo(company);
        List<TDrama> tDramas = tDramaMapper.selectByExample(example);
        return tDramas;
    }

    /**
     * 获取剧列表
     *
     * @param page
     * @param pageSize
     * @param title
     * @param startTime
     * @param endTime
     * @return
     */
    public Map findDramaList(int page, int pageSize, String title, String name, String startTime, String endTime) {
        Map pMap = new HashMap();

        pMap.put("page", page * pageSize);
        pMap.put("pageSize", pageSize);
        pMap.put("title", title);
        pMap.put("name", name);
        pMap.put("startTime", startTime);
        pMap.put("endTime", endTime);

        int cnt = tDramaSelfMapper.selectDramaCnt(pMap);

        List<TDramaWithBLOBs> list = tDramaSelfMapper.selectDramaList(pMap);

        Map dataMap = new HashMap();
        dataMap.put("list", list.size() == 0 ? null : list);
        dataMap.put("total", cnt);
        dataMap.put("page", page);
        return dataMap;
    }

    /**
     * 保存新剧
     */
    public int saveDrama(TDramaWithBLOBs drama) {
        drama.setId(GenerateKeyUtil.generationPK());
        drama.setCreateTime(DateUtil.getNow());
        drama.setUpdateTime(DateUtil.getNow());
        return tDramaMapper.insert(drama);
    }

    /**
     * 删除剧库
     */
    public void deleteDramaById(long id){
        TDramaCriteria example = new TDramaCriteria();
        TDramaCriteria.Criteria c = example.createCriteria();
        c.andIdEqualTo(id);
        tDramaMapper.deleteByExample(example);
    }

    /**
     * 更新剧库
     */
    public void updateDrama(TDramaWithBLOBs tDrama){
        // 更新时间
        tDrama.setUpdateTime(DateUtil.getNow());
        tDramaMapper.updateByPrimaryKeySelective(tDrama);
    }

    /**
     * 更新剧库状态
     * @param id
     * @param id
     */
    public void updateDramaStatus(Long id,String status){
        Map pMap = new HashMap();
        pMap.put("id",id);
        pMap.put("status", status);
        tDramaSelfMapper.updateDramaStatus(pMap);
    }
}
